package com.woniuxy.user.controller;

import com.woniuxy.commons.request.ResPage;
import com.woniuxy.commons.request.ResponseResult;
import com.woniuxy.user.dto.DepartmentDto;
import com.woniuxy.user.entity.Department;
import com.woniuxy.user.service.DepartmentService;
import com.woniuxy.user.vo.DepartmentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Resource
    private DepartmentService departmentService;

    /**
     * 获取部门列表
     */
    @PreAuthorize("hasAuthority('department:getList')")
    @GetMapping("/getDepartmentList")
    public ResponseResult<ResPage<Department>> getDepartmentList(DepartmentVo departmentVo) {
        return new ResponseResult<>(ResponseResult.OK, departmentService.getDepartmentList(departmentVo));
    }
    /**
     * 添加部门
     */
    @PostMapping("/addDepartment")
    public ResponseResult<Boolean> addDepartment(@RequestBody Department department) {
        return new ResponseResult<>(ResponseResult.OK, departmentService.addDepartment(department));
    }
    /**
     * 编辑部门
     */
    @PutMapping("/editDepartment")
    public ResponseResult<Boolean> editDepartment(@RequestBody Department department) {
        return new ResponseResult<>(ResponseResult.OK, departmentService.editDepartment(department));
    }
    /**
     * 启用部门
     */
    @PutMapping("/enableDepartment/{id}")
    public ResponseResult<Boolean> enableDepartment(@PathVariable("id") Integer id) {
        return new ResponseResult<>(ResponseResult.OK, departmentService.enableDepartment(id));
    }
    /**
     * 锁定部门
     */
    @PutMapping("/disableDepartment/{id}")
    public ResponseResult<Boolean> disableDepartment(@PathVariable("id") Integer id) {
        return new ResponseResult<>(ResponseResult.OK, departmentService.disableDepartment(id));
    }
    /**
     * 删除部门
     */
    @DeleteMapping("/deleteDepartment/{id}")
    public ResponseResult<Boolean> deleteDepartment(@PathVariable("id") Integer id) {
        return new ResponseResult<>(ResponseResult.OK, departmentService.deleteDepartment(id));
    }
    /**
     * 校验部门名称是否可用
     */
    @GetMapping("/checkName/{name}")
    public ResponseResult<Boolean> checkName(@PathVariable("name") String name) {
        return new ResponseResult<>(ResponseResult.OK, departmentService.checkName(name));
    }
    /**
     * 获取所有可用的部门列表
     */
    @GetMapping("/getAllDepartmentList")
    public ResponseResult<List<Department>> getAllDepartmentList() {
        return new ResponseResult<>(ResponseResult.OK, departmentService.getAllDepartmentList());
    }
    /**
     * 获取除用户部门以外所有可用的部门列表
     */
    @GetMapping("/getDepartmentListForEmployee")
    public ResponseResult<List<DepartmentDto>> getDepartmentListForEmployee() {
        return new ResponseResult<>(ResponseResult.OK, departmentService.getDepartmentListForEmployee());
    }
}
